jQuery(document).ready(function() {

    if (!Modernizr.svg) {
        $('img[src$=".svg"]').each(function() {
            $(this).attr('src', $(this).attr('src').replace('.svg', '.png'));
        });
    }

    $('[data-toggle="tooltip"]').tooltip()




    $('.carousel').carousel();

    // Goto Top

    $('#goTop').on('click', function(e) {
        $("html, body").animate({ scrollTop: $("#top").offset().top }, 1000);
    });

    // browser window scroll (in pixels) after which the "back to top" link is shown
    var offset = 300,
        //browser window scroll (in pixels) after which the "back to top" link opacity is reduced
        offset_opacity = 3200,
        //duration of the top scrolling animation (in ms)
        scroll_top_duration = 700,
        //grab the "back to top" link
        scroll_fixed_offset = 200,

        $back_to_top = $('.cd-top'),
        $navRoot = $('.nav-root');


    var $sticky = $navRoot.clone().addClass('sticky').insertAfter($navRoot);





    //hide or show the "back to top" link
    $(window).scroll(function() {
        var scrollTop = $(this).scrollTop();

        (scrollTop > offset) ? $back_to_top.addClass('cd-is-visible'): $back_to_top.removeClass('cd-is-visible cd-fade-out');
        if (scrollTop > offset_opacity) {
            $back_to_top.addClass('cd-fade-out');
        }

        if (scrollTop > scroll_fixed_offset) {
            $sticky.css('top', '0');
        } else {
            $sticky.removeAttr('style');
        }

    });

    $(window).trigger('scroll')

    //smooth scroll to top
    $back_to_top.on('click', function(event) {
        event.preventDefault();
        $('body,html').animate({
            scrollTop: 0,
        }, scroll_top_duration);
    });


    $(document).on('click', '[data-toggle="lightbox"]', function(event) {
        event.preventDefault();
        $(this).ekkoLightbox();
    });


    // Show More Show Less

    $(".show-more a").on("click", function() {
        var $link = $(this);
        var $content = $link.parent().prev("div.text-content");
        var linkText = $link.text();
        $content.show().slideUp('slow').toggleClass("short-text");
        $content.show().slideDown('slow').toggleClass("full-text");
        $link.text(getShowLinkText(linkText));
        return false;
    });

    function getShowLinkText(currentText) {
        var newText = '';
        if (currentText.toUpperCase() === "SHOW MORE") {
            newText = "Show Less ";
        } else {
            newText = "Show More";
        }
        return newText;
    }



    // Members Center Box Accordion



    // $('.accorinfo').on('shown.bs.collapse', 
    // function(){
    // $(this).find(".downico").removeClass("downico").addClass("upico");
    // })

    // .on('hidden.bs.collapse', 
    // 	function(){
    // $(this).find(".upico").removeClass("upico").addClass("downico");
    // });


    //products tab toggle

    $('.info-tab-item:eq(0)').addClass('cur');

    $('.info-tab-item').on('click', function() {
        $(this).addClass('cur').siblings().removeClass('cur');

        $('.pro').each(function() {
            var $el = $(this);
            $el.addClass('zoomOut');
            $el.one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
                $el.remove();
            });
        });

        var params = {
            cate_id: $(this).data('id')
        }
        if ($('body').hasClass('index-page')) {
            params.limit = 3
        }

        $.ajax({
            url: '/api/products.php',
            data: params
        }).done(function(res) {
            // console.log(res);

            setTimeout(function() {
                $('.pro-list').prepend(template('products', res));
                $('.pro.hide').show().addClass('zoomIn');

            }, 100)

        })

    });


    $('.about-page .tabs .btn').on('click', function() {
        $(this).addClass('cur').siblings().removeClass('cur');
        $('.aboutinfos .info-item').eq($(this).index()).fadeIn(700).siblings().hide();
    });



    // Members Tabactive Section
    var selector = '.tabactive ul a';
    $(selector).on('click', function() {
        $(selector).removeClass('active');
        $(this).addClass('active');
    });

    // Home Page Slider Section
    $('.centerbox div').velocity("transition.slideUpIn", { delay: 200, stagger: 400, drag: true });
    $('.line').velocity("transition.fadeIn", { delay: 500, duration: 1100 });
    $('.qualogo img').velocity("transition.slideUpIn", { delay: 400, duration: 1500 });
    $('.productinfo .topinfo').velocity("transition.slideDownIn", { delay: 400, stagger: 400, drag: true });
    $('.productinfo  .subinfo').velocity("transition.slideDownIn", { delay: 600, stagger: 400, drag: true });
    $('.brands .col-md-2').velocity("transition.slideDownIn", { delay: 800, stagger: 400, drag: true });
    $('.infotext div')
        .velocity("transition.slideDownIn", { delay: 800, stagger: 200, drag: true });
    // $('.qualogo img').velocity("transition.slideUpIn", {stagger: 3000, drag: true });


    //  About Us
    $('.aboutheader .topinfo div').velocity("transition.slideUpIn", { stagger: 400, drag: true });
    $('.aboutinfo .info div').velocity("transition.slideUpIn", { delay: 800, stagger: 400, drag: true });
    $('.aboutmsg .info div').velocity("transition.slideUpIn", { delay: 800, stagger: 400, drag: true });


    //  Contact Us
    $('.contactinfo div').velocity("transition.slideUpIn", { stagger: 400, drag: true });
    $('.contactmsg .info div').velocity("transition.slideUpIn", { delay: 1000, stagger: 400, drag: true });

    // Product
    $('.productheader .topinfo div').velocity("transition.slideUpIn", { stagger: 400, drag: true });
    $('.product .s1 ').velocity("transition.slideUpIn", { delay: 800, stagger: 400, drag: true });
    $('.productmain .row .col-12 ').velocity("transition.slideUpIn", { delay: 800, stagger: 500, drag: true });
    $('.productmain .mask ').velocity("transition.slideUpIn", { delay: 800, stagger: 800, drag: true });
    $('.productmain ').velocity("transition.slideUpIn", { delay: 800, stagger: 800, drag: true });



    $('#message div').velocity("transition.slideDownIn", { stagger: 400, drag: true })
        .velocity("transition.slideUpOut", { delay: 4000, stagger: 400, drag: true });







});


//全局处理错误的图片
document.addEventListener("error", function(e) {
    var elem = e.target;
    if ((elem.tagName.toLowerCase() === 'img') && !elem.onerror) {
        elem.src = "";
    }
}, true);